package yongxin.service.impl.tasks;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import yongxin.entity.tasks.TasksEntity;
import yongxin.service.tasks.TasksServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.web.system.service.SystemService;


@Service("tasksService")
@Transactional
public class TasksServiceImpl extends CommonServiceImpl implements TasksServiceI {
    
	@Autowired
	private SystemService systemService;
	
	/*
	 * 判断工序是否结束(non-Javadoc)
	 * 
	 */
	public boolean checkISOver(String fbcph) {
		boolean result = true;
		
		String sql =  " select count(1) counts from (select fbcph from t_processreport ";
		       sql += " where fbcph like ? group by fbcph ) a";
		       sql += " where a.fbcph not in (";
		       sql += " select fbcph from t_process where fbcph like ?";
		       sql += " and fprocess='over' )";
		
		List<Map<String,Object>> list = systemService.findForJdbc(sql, fbcph+"%",fbcph+"%");    
		int counts = (Integer) list.get(0).get("counts");
		if(counts > 0){
			result = false;
		}
		return result;
	}
    
	/*
	 * 计算产出重量总和 (non-Javadoc)
	 * 
	 */
	public double computeCCZL(String fbcph) {
		String sql =  " select sum(a.zl) cc_sum from ( ";
		       sql += " select sum(convert(numeric(19,2),ffqzl)) zl from t_cut ";
		       sql += " where fbcph like ? and ffqzl!='' and nprocess = 'over' ";
		       sql += " union all ";
		       sql += " select sum(convert(numeric(19,2),ffqzl)) zl from t_chromate ";
		       sql += " where fbcph like ? and ffqzl!='' and nprocess = 'over' ";
		       sql += " union all ";
		       sql += " select sum(convert(numeric(10,2),fcczl)) zl  ";
		       sql += " from  t_processreport where fbcph like ? and nprocess = 'over' ";
		       sql += " ) a ";
		       
		List<Map<String, Object>> list = systemService.findForJdbc(sql, fbcph+"%",fbcph+"%",fbcph+"%");
		BigDecimal cc_sum = (BigDecimal) list.get(0).get("cc_sum");
		double c_sum = cc_sum.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
		return c_sum;
	}
	
	
	/*
	 * 查任务单投入重量
	 */
	
	public double selectTRZL(String fbcph){
		String hql = " from TasksEntity where fbcph = ?";
		
		List<TasksEntity> list = systemService.findHql(hql,fbcph);
		double trzl = Double.parseDouble(list.get(0).getFweight());
		return trzl;
	}
	
	/*
	 * 更新生产任务单成材率 
	 */
	
	
	public void computeRate(String rate,String fbcph){
		
		Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = df.format(date);
		String sql = " update t_tasks set frate = ?,fstate = '1',update_date = ? where fbcph = ? ";
		int row = systemService.executeSql(sql,rate,dateStr,fbcph);
		
	}
	
	
	// 两个double类型相除
	public  String div(Double v1, Double v2) {  
	/*	   
		   BigDecimal b1 = new BigDecimal(v1.toString());  
		   BigDecimal b2 = new BigDecimal(v2.toString());  
		   double rate =  new Double(b1.divide(b2, 3, BigDecimal.ROUND_HALF_UP).doubleValue());  */
		   
		   NumberFormat formatter = new DecimalFormat("0.00");
		   Double x=new Double(v1/v2); 
		   String rate = formatter.format(x * 100);
		   
	       return (rate + "%");
		} 
	
}